package com.example.myapplicationktkt2.utils

import android.os.Build
import android.util.Log
import java.io.File

/**
 * 模拟器检测工具类
 * 用于检测应用是否在模拟器上运行
 */
class EmulatorDetector {
    companion object {
        private const val TAG = "EmulatorDetector"

        /**
         * 检测是否在模拟器上运行
         * @return true表示在模拟器上运行，false表示在真机上运行
         */
        fun isEmulator(): Boolean {
            // 检查是否包含模拟器特征
            return checkBasicEmulatorFeatures() || 
                   checkAdvancedEmulatorFeatures() || 
                   checkEmulatorFiles()
        }

        /**
         * 检查基本的模拟器特征
         */
        private fun checkBasicEmulatorFeatures(): Boolean {
            return (Build.FINGERPRINT.startsWith("generic") ||
                    Build.FINGERPRINT.startsWith("unknown") ||
                    Build.MODEL.contains("google_sdk") ||
                    Build.MODEL.contains("Emulator") ||
                    Build.MODEL.contains("Android SDK built for x86") ||
                    Build.MANUFACTURER.contains("Genymotion") ||
                    Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic") ||
                    "google_sdk".equals(Build.PRODUCT))
        }

        /**
         * 检查高级模拟器特征
         */
        private fun checkAdvancedEmulatorFeatures(): Boolean {
            return (Build.HARDWARE.contains("goldfish") ||
                    Build.HARDWARE.contains("ranchu") ||
                    Build.HARDWARE.contains("vbox86") ||
                    Build.BOARD.contains("unknown") ||
                    Build.BOARD.contains("goldfish") ||
                    Build.BOARD.contains("vbox86") ||
                    Build.BOOTLOADER.contains("unknown"))
        }

        /**
         * 检查模拟器特有的文件
         */
        private fun checkEmulatorFiles(): Boolean {
            val emulatorFiles = arrayOf(
                "/system/bin/qemu-props",
                "/dev/socket/qemud",
                "/dev/qemu_pipe"
            )

            for (file in emulatorFiles) {
                if (File(file).exists()) {
                    Log.d(TAG, "检测到模拟器文件: $file")
                    return true
                }
            }
            return false
        }
    }
}